package com.example.demo.alex.controller;

import com.example.demo.alex.model.Person;
import com.example.demo.alex.service.PersonService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @ClassName PersonController
 * @Description TODO
 * @Author zjg
 * @Date 2018/12/2516:06
 * @Version 2.0
 */

@Controller
public class PersonController {

    @Autowired
    PersonService personService;
    @Autowired(required = false)
    private AuthenticationManager myAuthenticationManager;

//    @GetMapping("/login")
//    public String loign(){
//        return "login";
//    }
//
//    @GetMapping("/login")
//    public String userLogin(HttpServletRequest request) {
//
//        Person person=new Person();
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
//       // String kaptcha = request.getParameter("kaptcha");
//
//        person.setUserName(username);
//        person.setPwd(password);
//   //     String s = request.getSession().getAttribute(ConstantVal.CHECK_CODE).toString();
////        if (StringUtils.isEmpty(kaptcha) || !s.equals(kaptcha)) {
////            return "redirect:login-error?error=1";
////        }
//
//        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(username, password);
//
//
//        try{
//            //使用SpringSecurity拦截登陆请求 进行认证和授权
//            Authentication authenticate = myAuthenticationManager.authenticate(usernamePasswordAuthenticationToken);
//
//            SecurityContextHolder.getContext().setAuthentication(authenticate);
//            //使用redis session共享
//            HttpSession session = request.getSession();
//            session.setAttribute("SPRING_SECURITY_CONTEXT", SecurityContextHolder.getContext()); // 这个非常重要，否则验证后将无法登陆
//        }catch (Exception e){
//            e.printStackTrace();
//            return "redirect:login-error?error=2";
//        }
//
//
//        return "redirect:index";
//    }


    @GetMapping("/getAllUser")
    public Person findByUserName(@Param("userName")String userName){
      Person person=  personService.findUserByUserName(userName);
      return person;
    }

}
